package html;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import org.jsoup.Connection;
import org.jsoup.HttpStatusException;
import org.jsoup.Jsoup;
import org.jsoup.UnsupportedMimeTypeException;
import org.jsoup.helper.HttpConnection;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class ExtractTag implements IExtractTag{

	public ArrayList<String> getAllLinks (String baseUrl) 
			throws IOException{
		
		ArrayList<String> links = new ArrayList<String> ();
		
		
		String filter="";
		
		filter = baseUrl.substring(0,baseUrl.indexOf("://")+3);
	    
		String filter1 = baseUrl.substring(baseUrl.indexOf("://")+3);
		
		if(filter1.indexOf("/")>0){
			filter = baseUrl.substring(0,baseUrl.indexOf("://")+3) + filter1.substring( 0, filter1.indexOf("/"));
		}else{
			filter = baseUrl;
		}
	    
		
		
		//System.out.println(filter);
		//System.out.println( baseUrl.substring(0,baseUrl.indexOf("://")+3) + filter1.substring( 0, filter1.indexOf("/")));
		//System.exit(0);
		
	   // String dir = System.getProperty("user.dir");
	    
	    //folders = dir+folders.substring(0,folders.lastIndexOf("/"));
	    
	    
	    
		//String filter = baseUrl;//"http://dados.recife.pe.gov.br";
		
		Connection con = Jsoup.connect(baseUrl).userAgent("Mozilla/5.0 (X11; "
       		+ "Linux x86_64) AppleWebKit/535.21 (KHTML, like Gecko) "
       		+ "Chrome/19.0.1042.0 Safari/535.21").timeout(10000);
       
		Connection.Response resp = con.execute();
		Document doc = null;
        
		if (resp.statusCode() == 200) {
            doc = con.get();
            
          //Document doc = Jsoup.connect(URL).get();
          Elements questions = doc.select("a[href]");
   
          for (Element link : questions) {
         	//!link.attr("abs:href").equals("") && 
        	  String url = link.attr("abs:href");
        	  
        	  if( url.length()>= filter.length()){
        		  
        		  	//System.out.println("-----------"+url );
					if( url.substring(0, filter.length()).equals(filter) ){
						
						//System.out.println("===="+url);
						
						links.add(url);
						
					}
        		  
        	  }
        	  
        	 
          		
       
          }
            
        }else{
        	throw new IOException("HTTP Status Code "+resp.statusCode());
        }
		

        return links;
	
	}
	
}
